/**
 * Copyright (c) 2017-2022 Rushang Co.Ltd All Rights Reserved.
 */
package com.rshang.rsdp.router;

import com.rshang.rsdp.entity.TbRsdlRyxx;
import com.rshang.rsdp.service.ITbRsdlRyxxService;
import com.rshang.rsdp.service.ITbRsdlTzjlService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Date;

/**
 * 功能简介：合同路由控制器<br/>
 *
 * @author zhangjun
 * @create 2017年8月23日上午9:39:43
 */
@Controller
@RequiresAuthentication
@RequestMapping("/htxx")
public class HtRouter extends BaseRouter {

    @Autowired
    ITbRsdlTzjlService tzjlService;

    @Autowired
    ITbRsdlRyxxService ryService;

    @RequestMapping("/ybcx")
    public ModelAndView ybcx() {
        ModelAndView view = new ModelAndView("htxx/ybcx_search");
        view.addObject("dqrq", new Date());
        return view;
    }

    @RequestMapping("/tzxx")
    public ModelAndView tzjl() {
        return new ModelAndView("htxx/tzjl_index");
    }

    @RequestMapping("/tzxx/add")
    public ModelAndView tzjlAdd() {
        return new ModelAndView("htxx/tzjl_add");
    }

    @RequestMapping("/tzxx/edit")
    public ModelAndView tzxxEdit() {
        ModelAndView view = new ModelAndView("htxx/tzjl_edit");
        view.addObject("result", tzjlService.get(request));
        return view;
    }

    /**
     * 模板导入
     */
    @RequestMapping("/tzxx/import")
    public ModelAndView tzxxImport() {
        return new ModelAndView("htxx/tzjl_import");
    }

    @RequestMapping("/tzxx/download")
    public ResponseEntity<FileSystemResource> tzxxDownload() throws IOException {
        HttpHeaders header = new HttpHeaders();
        header.setContentType(new MediaType("application", "vnd.ms-excel"));
        header.set("Content-disposition", "attachment;filename=tzxx-v1.0.xls");
        Resource resource = new ClassPathResource("xls/tzxx-v1.0.xls");
        return new ResponseEntity<>(new FileSystemResource(resource.getFile()), header, HttpStatus.OK);
    }

    @RequestMapping("/ygxx")
    public ModelAndView ygxx() {
        return new ModelAndView("htxx/htxx_index");
    }

    @RequestMapping("/add")
    public ModelAndView ygxxAdd() {
        ModelAndView view = new ModelAndView("htxx/htxx_add");
        view.addObject("bclx", "0");
        return view;
    }

    @RequestMapping("/ryBgjs")
    public ModelAndView ryBgjs() {
        ModelAndView view = new ModelAndView("htxx/htxx_bgjs");
        view.addObject("id", request.getParameter("id"));
        String mc = request.getParameter("mc");
        view.addObject("mc", mc.substring(0, mc.length() - 1));
        return view;
    }
    
    @RequestMapping("/rylz")
    public ModelAndView rylz() {
        ModelAndView view = new ModelAndView("htxx/htxx_lz");
        view.addObject("id", request.getParameter("id"));
        String mc = request.getParameter("mc");
        view.addObject("mc", mc.substring(0, mc.length() - 1));
        return view;
    }

    @RequestMapping("/edit")
    public ModelAndView edit() {
        ModelAndView view = new ModelAndView("htxx/htxx_edit");
        TbRsdlRyxx ryxx = ryService.get(request);
        view.addObject("result", ryxx);
        view.addObject("ryhis", ryService.getRyHistory(ryxx.getRyId()));
        return view;
    }

    @RequestMapping("/copy")
    public ModelAndView copy() {
        ModelAndView view = new ModelAndView("htxx/htxx_copy");
        view.addObject("result", ryService.get(request));
        view.addObject("bclx", "1");
        return view;
    }

    @RequestMapping("/import")
    public ModelAndView ryxxImport() {
        return new ModelAndView("htxx/htxx_import");
    }

    @RequestMapping("/download")
    public ResponseEntity<FileSystemResource> download() throws IOException {
        HttpHeaders header = new HttpHeaders();
        header.setContentType(new MediaType("application", "vnd.ms-excel"));
        header.set("Content-disposition", "attachment;filename=ryxx-v1.0.xls");
        Resource resource = new ClassPathResource("xls/ryxx-v1.0.xls");
        return new ResponseEntity<>(new FileSystemResource(resource.getFile()), header, HttpStatus.OK);
    }
    
    /**
     * 新增小公司
     */
    @RequestMapping("/gsAdd")
    public ModelAndView gsAdd() {
        return new ModelAndView("system/xgs_add2");
    }

}
